package com.bookshop.service.impl;

import com.bookshop.entity.Book;
import com.bookshop.entity.Cart;
import com.bookshop.entity.CartWithBook;
import com.bookshop.mapper.CartMapper;
import com.bookshop.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    private CartMapper cartMapper;
    @Autowired
    public CartServiceImpl(CartMapper cartMapper) {
        this.cartMapper = cartMapper;
    }

    @Override
    public int addToCart(Cart cart) {
        //查询用户是否已经添加了该图书
        Integer book_nums = cartMapper.searchCart(cart.getUser_id() , cart.getBook_id());
        int result = 0;
        //数量为0，用户没有添加，插入新的字段
        if(book_nums == null)
        {
            result = cartMapper.addToCart(cart.getUser_id() , cart.getBook_id() , cart.getBook_nums());
        }
        else
        {
            result = cartMapper.updateNums(cart.getUser_id() , cart.getBook_id() , cart.getBook_nums() + book_nums);
        }
        //result = cartMapper.addToCart(cart.getId() , cart.getUser_id() , cart.getBook_id() , cart.getBook_nums());

        return result;
    }

    @Override
    public int updateCart(Cart cart) {
        int result = cartMapper.updateNums(cart.getUser_id() , cart.getBook_id() , cart.getBook_nums());
        return result;
    }

    @Override
    public int deleteFromCart(int userId , List<Integer> bookIds)
    {
        int result = 0;

        for(int i = 0 ; i < bookIds.size() ; i++)
        {
            result = cartMapper.deleteFromCart(userId , bookIds.get(i));
        }

        return result;
    }

    @Override
    public ArrayList<CartWithBook> searchAllCart(Integer userId) {
        List<Book> books = cartMapper.selectAllCart(userId);
        System.out.println(books);
        ArrayList<CartWithBook> booksByPublisher = new ArrayList<>();
        boolean flag = false;

        for(Book book : books)
        {
            for (CartWithBook cartWithBook : booksByPublisher){
                if(cartWithBook.getPublish_name().equals(book.getPublish()))
                {
                    cartWithBook.getBooks().add(book);
                    flag = true;
                    break;
                }
            }
            if(flag == false)
            {
                CartWithBook cartWithBook = new CartWithBook(book.getPublish() , new ArrayList<Book>());
                cartWithBook.getBooks().add(book);
                booksByPublisher.add(cartWithBook);
            }
            flag = false;
        }
        System.out.println(booksByPublisher);
        return booksByPublisher;
    }
    @Override
    public int addToFavorite(int userId, int bookId) {
        int result = cartMapper.addToFavorite(userId , bookId);
        if(result == 0)
        {
            return 0;
        }

        result = cartMapper.deleteFromCart(userId , bookId);
        return result;
    }
}
